/**
 * 
 */
package au.edu.swin.videoshop.posapp.ui.sections;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.component.UIData;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import au.edu.swin.videoshop.bean.Customer;
import au.edu.swin.videoshop.bean.Rental;
import au.edu.swin.videoshop.bean.TitleCopy;
import au.edu.swin.videoshop.service.CustomerService;
import au.edu.swin.videoshop.service.RentalService;
import au.edu.swin.videoshop.service.TitleService;

/**
 * @author julie
 *
 */
public class RentalSection implements Serializable {
	private Long mCustomerId;
	private String mTitleCopyId;
	private Customer mCustomer;
	private List mRentals;
	private Map mTitleCopyById;
	private BigDecimal mTotalPrice;
	
	@EJB
	private CustomerService mCustomerService;
	
	@EJB
	private RentalService mRentalService;
	
	@EJB
	private TitleService mTitleService;
	
	public RentalSection() {
	}

	/**
	 * @return the customerId
	 */
	public String getCustomerId() {
		if (mCustomerId != null) {
			return mCustomerId.toString();
		}
		else {
			return null;
		}
	}

	/**
	 * @param customerId the customerId to set
	 */
	public void setCustomerId(String customerId) {
		try {
			mCustomerId = new Long(customerId);
		}
		catch (NumberFormatException ex) {
			mCustomerId = null;
		}
	}

	/**
	 * @return the customer
	 */
	public Customer getCustomer() {
		return mCustomer;
	}

	/**
	 * @return the rentals
	 */
	public List getRentals() {
		return mRentals;
	}

	/**
	 * @return the totalPrice
	 */
	public BigDecimal getTotalPrice() {
		return mTotalPrice;
	}

	/**
	 * @return the titleCopyId
	 */
	public String getTitleCopyId() {
		return mTitleCopyId;
	}

	/**
	 * @param titleCopyId the titleCopyId to set
	 */
	public void setTitleCopyId(String titleCopyId) {
		mTitleCopyId = titleCopyId;
	}

	public String searchCustomer() {
		return null;
	}
	
	public String start() {
		mTitleCopyId = null;
		mRentals = null;
		mTitleCopyById = null;
		mTotalPrice = BigDecimal.ZERO;
		
		if (mCustomerId != null) {
			mCustomer = mCustomerService.findCustomerByCustomerId(mCustomerId);
			
			if (mCustomer != null) {
				mRentals = new ArrayList();
				mTitleCopyById = new HashMap();
			}
			
			mCustomerId = null;
		}
		
		return null;
	}
	
	public String add() {
		if (mTitleCopyId != null) {
			FacesContext facesContext = FacesContext.getCurrentInstance();
			UserSection userSection = (UserSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{userSection}", UserSection.class);

			TitleCopy copy = mTitleService.findTitleCopyByTilteCopyId(mTitleCopyId);
			
			if (copy != null && !mTitleCopyById.containsKey(copy.getTitleCopyId())) {
				Rental rental = mRentalService.makeRental(mCustomer, copy, userSection.getStaff(), new java.util.Date());
				
				if (rental != null) {
					mRentals.add(rental);
					mTitleCopyById.put(copy.getTitleCopyId(), copy);
					mTotalPrice = mTotalPrice.add(rental.getFirstCharge());
				}
			}
			
			mTitleCopyId = null;
		}
		return null;
	}

	
	public String delete() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("rentalForm").findComponent("rentalTable");
		Rental rental = (Rental) table.getRowData();
		
		mRentals.remove(rental);
		mTitleCopyById.remove(rental.getTitleCopy().getTitleCopyId());
		mTotalPrice = mTotalPrice.subtract(rental.getFirstCharge()); 
		
		return null;
	}

	public String finish() {
		if (mRentals != null && !mRentals.isEmpty()) {
			for (Iterator i = mRentals.iterator(); i.hasNext();) {
				mRentalService.addRental((Rental)i.next());
			}
			
			clear();
			
			return "finish";
		}
		
		return null;
	}
	
	public String cancel() {
		clear();
		
		return null;
	}
	
	public String clear() {
		mCustomerId = null;
		mTitleCopyId = null;
		mCustomer = null;
		mRentals = null;
		mTitleCopyById = null;
		mTotalPrice = BigDecimal.ZERO;
		
		return null;
	}
}

